import os

def list_files_in_folder(folder_path):
    # 获取文件夹中的所有文件名
    files = os.listdir(folder_path)
    files = sorted(files)
    # 过滤出文件名，而非文件夹名
    files = [file for file in files if os.path.isfile(os.path.join(folder_path, file))]

    return files

def save_to_txt(file_list, output_txt_path):
    with open(output_txt_path, 'w') as file:
        for filename in file_list:
            file.write(f"./images/train/{filename}" + '\n')

# 指定文件夹路径
folder_path = 'images/train/'

# 获取文件夹中的文件名列表
files_in_folder = list_files_in_folder(folder_path)

# 指定输出txt文件路径
output_txt_path = 'train.txt'

# 将文件名保存到txt文件中
save_to_txt(files_in_folder, output_txt_path)

print(f"File names have been saved to {output_txt_path}")